package com.frxuexi.user.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.user.R
import com.frxuexi.user.entity.ApproveEntity
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.viewmodel.UserNetViewModel
import com.frxuexi.user.viewmodel.UserViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch

/**
 * 请假列表页面
 */
@Composable
fun ApproveListPage(navHostController: NavHostController, vm: UserNetViewModel = hiltViewModel(),vm2:UserViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<ApproveEntity>()
    }

//    val list = vm2.getData().collectAsLazyPagingItems()

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        list.clear()
                        Log.i("===", it.data.toString())
                        list.addAll(it.data as Collection<ApproveEntity>)
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(UserIntent.getApprs("未审核"))
    })


    Column(Modifier.padding(10.dp)) {
        Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            Icon(
                imageVector = Icons.Default.ArrowBack,
                contentDescription = "",
                Modifier.clickable { navHostController.popBackStack() })
            Text(text = "请假列表")
            Text(
                text = "申请",
                Modifier
                    .background(colorResource(id = R.color.orange), RoundedCornerShape(5.dp))
                    .padding(10.dp, 3.dp)
                    .clickable {
                        navHostController.navigate(RoutePath.ADDAPPROVE)
                    }
            )
        }
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 16.dp), horizontalArrangement = Arrangement.SpaceAround
        ) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list) {

                Row(
                    Modifier
                        .fillMaxWidth()
                        .background(
                            colorResource(id = R.color.lightgray),
                            RoundedCornerShape(10.dp)
                        )
                        .padding(vertical = 16.dp)
                        .clickable {
                            val json = Gson().toJson(it)
                            if (it!!.status == "通过" || it.status == "拒绝") {
                                navHostController.navigate(RoutePath.APPROVEDETAIL + "/$json")
                            } else {
                                navHostController.navigate(RoutePath.SP + "/$json")
                            }
                        },
                    horizontalArrangement = Arrangement.SpaceAround
                ) {
                    Text(text = it!!.type)
                    Text(text = it.startTime.substring(0, 10))
                    Text(text = it.endTime.substring(0, 10))
                    val color =
                        if (it.status == "通过") Color.Green else if (it.status == "拒绝") Color.Red else Color.LightGray
                    Text(
                        text = it.status,
                        Modifier
                            .background(color, RoundedCornerShape(5.dp))
                            .padding(5.dp)
                    )
                }
            }
        })

    }
}
